package run;

import processing.core.PImage;
import processing.core.PVector;
import static java.lang.Math.*;

public class FigureCell extends LineCell {

	public FigureCell(PVector initPos, PVector initDirection, int gen) {
		super(initPos, initDirection, gen);
	}

	@Override
	protected void linearStrokeDensity(PVector curPos, PVector origPos) {
	    
	    float x = curPos.x;
	    float y = curPos.y;
	    
	    // TODO have some of these be random variables...
	    //TODO some start weak and end strong, others start strong and end weak...
	    // perhaps tie it to their proximity to other lines!!!
	    
	    maxWidth = 4f; 
	    float max_dens = 30;
	    float min_dens = 5;
	    float reduct_rate = 0.5F;
	    float dens = max(max_dens - reduct_rate * (curPos.dist(origPos)), min_dens);
	    
	    int numNearbyCells = Env.instance.getNearbyCells(this, 20).size();
		dens += (numNearbyCells + 2) * 4F ;

	    float width = castToRange(1F, maxWidth, min_dens, max_dens, dens);
	    width -= (numNearbyCells + 1);
	    if (width < 0.5) width = 0.5F;
	    
	    int point_count = (int) (width * 2);
	    
	    //TODO another set of (+) terms accounting for nearby cell distances
	    point_count = (int) ((numNearbyCells + 2) * 5F) / (generation + 1) ;

	    for (int i = 0; i < point_count; i++) {

	    	// ALSO depend upon generation(a bit) & dist from nearest neighbors...
	    	// alpha smaller the greater the width
	    	float alpha = 10 + 70 / (width + 0.45F*generation*generation + 3*numNearbyCells );
	    	
	    	float figureDist = Env.instance.distToNearestFigurePt(loc);
	    	// closer to figurepoint, greater alpha
	    	if (figureDist < 2) {
	    		alpha = 200;
	    	}
	    	else alpha = 1;
//	    	if (alpha < 0) alpha = 0;
			p.stroke(r,g,b, alpha);
		    p.point(x+p.random(-width,width),y+p.random(-width,width));
	    }

	    if (numNearbyCells < 3) {
	    	// TODO blurs a portion of the image; use this in a better place!!!
		    int bw = (int) (2 + width);
		    PImage pim = p.get((int) (loc.x - bw), (int) (loc.y - bw), (int)bw*2, (int)bw*2);
			pim.filter(p.BLUR, 0.6F);
			p.set((int) (loc.x - bw), (int) (loc.y - bw), pim);
	    }
	    

	}
}
